package cn.tedu.pet.product.controller;

import cn.tedu.pet.commons.web.JsonResult;
import cn.tedu.pet.product.pojo.param.CategoryAddNewParam;
import cn.tedu.pet.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.pet.product.pojo.vo.CategoryListItemVO;
import cn.tedu.pet.product.pojo.vo.CategoryStandardVO;
import cn.tedu.pet.product.pojo.vo.CategoryTreeItemVO;
import cn.tedu.pet.product.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/categories")
@Validated
@Api(tags = "01. 类别管理模块")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam){
        log.debug("开始处理【添加类别】的请求，参数：{}",categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "类别ID",required = true,dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除类别失败，请提交合法的ID值！")
                             @RequestParam Long categoryId){
        log.debug("开始处理【删除类别】的请求，参数：{}", categoryId);
        categoryService.delete(categoryId);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改类别模板")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "类别模板ID",required = true,dataType = "long")
    })
    public JsonResult update(@Range(min = 1,message = "请提交有效的类别模板ID值！")@RequestParam Long categoryId,
                             @Valid CategoryUpdateInfoParam categoryUpdateInfoParam){
        log.debug("开始处理【修改类别模板详情】的请求，ID：{}，新数据：{}",categoryId,categoryUpdateInfoParam);
        categoryService.updateInfoById(categoryId,categoryUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<CategoryStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【根据ID查询类别详情】的请求，参数：{}", id);
        CategoryStandardVO queryResult = categoryService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/list")
    @ApiOperation("查询类别列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list(Long parentId) {
        log.debug("开始处理【查询品牌列表】的请求，页码：{}", parentId);
        List<CategoryListItemVO> pageData = categoryService.list(parentId);
        return JsonResult.ok(pageData);
    }


    // http://localhost:9080/categories/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/display
    @PostMapping("/{id:[0-9]+}/display")
    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisplay(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【显示类别】的请求，参数：{}", id);
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/hidden
    @PostMapping("/{id:[0-9]+}/hidden")
    @ApiOperation("隐藏类别")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setHidden(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【隐藏类别】的请求，参数：{}", id);
        categoryService.setHidden(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/tree
    @GetMapping("/tree")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 421)
    public JsonResult<List<CategoryTreeItemVO>> listTree() {
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<CategoryTreeItemVO> categoryTree = categoryService.listTree();
        return JsonResult.ok(categoryTree);
    }

}
